package com.smedi.ismedi.cronjob.core.infrastructure.util;

import okhttp3.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Component
public class HttpUtil {
    private final MediaType mediaType = MediaType.get("application/json; charset=utf-8");

    OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(60 * 1000, TimeUnit.MILLISECONDS)
            .readTimeout(5 * 60 * 1000, TimeUnit.MILLISECONDS)
            .writeTimeout(5 * 60 * 1000, TimeUnit.MILLISECONDS)
            .build();

    public String post(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(json, mediaType);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }
}
